package com.pokermind.repository.mapper;

import com.pokermind.model.po.PokerHand;
import org.springframework.jdbc.core.RowMapper;

import java.math.BigDecimal;
import java.sql.ResultSet;
import java.sql.SQLException;

/**
 * 扑克手牌行映射器
 * 对应 poker_hands 表
 * 
 * @author PokerMind Team
 */
public class PokerHandRowMapper implements RowMapper<PokerHand> {
    
    @Override
    public PokerHand mapRow(ResultSet rs, int rowNum) throws SQLException {
        PokerHand hand = new PokerHand();
        
        // 基础信息
        hand.setId(rs.getLong("id"));
        hand.setHandId(rs.getString("hand_id"));
        hand.setTableId(rs.getString("table_id"));
        hand.setUserId(rs.getLong("user_id"));
        
        // 牌局配置
        hand.setButtonSeat(rs.getInt("button_seat"));
        hand.setSmallBlind(rs.getBigDecimal("small_blind"));
        hand.setBigBlind(rs.getBigDecimal("big_blind"));
        
        BigDecimal ante = rs.getBigDecimal("ante");
        if (ante != null) {
            hand.setAnte(ante);
        }
        
        hand.setPlayersDealt(rs.getInt("players_dealt"));
        
        // 牌局状态
        hand.setCurrentStreet(rs.getString("current_street"));
        hand.setBoardCards(rs.getString("board_cards"));
        hand.setCurrentPot(rs.getBigDecimal("current_pot"));
        hand.setStatus(rs.getString("status"));
        
        // 结果信息
        BigDecimal finalPot = rs.getBigDecimal("final_pot");
        if (finalPot != null) {
            hand.setFinalPot(finalPot);
        }
        
        BigDecimal rakeAmount = rs.getBigDecimal("rake_amount");
        if (rakeAmount != null) {
            hand.setRakeAmount(rakeAmount);
        }
        
        // ✅ V4.8新增：最终结果JSON
        String finalResults = rs.getString("final_results");
        if (finalResults != null) {
            hand.setFinalResults(finalResults);
        }
        
        // ✅ V4.5新增：Run It Twice字段
        try {
            Boolean runItTwice = rs.getBoolean("run_it_twice");
            if (!rs.wasNull()) {
                hand.setRunItTwice(runItTwice);
            }
        } catch (SQLException e) {
            // 字段可能不存在（旧数据库），忽略
        }
        
        try {
            String runTwiceDetails = rs.getString("run_twice_details");
            if (runTwiceDetails != null) {
                hand.setRunTwiceDetails(runTwiceDetails);
            }
        } catch (SQLException e) {
            // 字段可能不存在（旧数据库），忽略
        }
        
        // 时间信息 - 使用Long时间戳
        hand.setCreateTime(getLongOrNull(rs, "create_time"));
        hand.setUpdateTime(getLongOrNull(rs, "update_time"));
        hand.setCompletedTime(getLongOrNull(rs, "completed_time"));
        
        // 玩家信息JSON - 完整玩家信息
        hand.setPlayersInfo(rs.getString("players_info"));
        
        return hand;
    }
    
    /**
     * 安全获取Long值，处理NULL情况
     */
    private Long getLongOrNull(ResultSet rs, String columnName) throws SQLException {
        long value = rs.getLong(columnName);
        return rs.wasNull() ? null : value;
    }
}